<?php 

include('SimFile.class.php');
header("Content-Type: text/html; charset=utf-8");
$basedir = "D:\package";

$files = SimFile::getSubFile($basedir);
$list = array();
$act = isset($_GET['act']) ? $_GET['act'] : '';

if($files){
    foreach ($files as $k=>$v) {
        $file_arr = pathinfo($v);
        $list[$k]['path'] = $v;
        $list[$k]['name'] = $file_arr['basename'];
        $list[$k]['size'] = SimFile::getFileSize($v);
    }
}

if ('download' == $act) {
    $path = $_GET['path'] ? $_GET['path']:'';
    if(empty($path)){
        echo "<script>alert('下载路径不存在！');location.reload();</script>";exit;
    }
    $pathinfo_arr = pathinfo($path);
    $filename = $pathinfo_arr['basename'];

    header('Content-Disposition: attachment; filename='.$filename);
    header('Content-Type: application/vnd.iphone');
    header('Content-Length: '.filesize($path)); 
    header('Content-Transfer-Encoding: binary'); 
    header('Cache-Control: must-revalidate'); 
    header('Pragma: public'); 
    readfile($path);
}

?>
<!DOCTYPE html>
<html lang="en" >
<head>
    <meta charset="UTF-8">
    <!-- Latest compiled and minified CSS and JS -->
    <link rel="stylesheet" href="http://cdn.amazeui.org/amazeui/2.2.1/css/amazeui.min.css">
    <script src="http://libs.useso.com/js/jquery/2.1.1/jquery.min.js"></script>
    <script src="http://cdn.amazeui.org/amazeui/2.2.1/js/amazeui.min.js"></script>
    <title>title</title>
</head>
<body>
    <div id="header"></div>
    <div id="xf-container">
        <div class="am-g am-g-fixed">
            
                <table class="am-table">
                    <thead>
                        <tr>
                            <th>文件名称</th>
                            <th>大小</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                            foreach ($list as $key => $value) {
                             
                        ?>
                        <tr>
                            <td><?php echo $value['name'] ?></td>
                            <td><?php echo $value['size'] ?></td>
                            <td><a href="?act=download&path=<?php echo $value['path'] ?>" class="am-btn am-btn-primary am-btn-sm"><i class="am-icon-cloud-download"></i>下载</a></td>
                        </tr>
                        <?php } ?>
                    </tbody>
                </table>
            
        </div>
    </div>
    
</body>
</html>